﻿#region Copyright(C) 2018 FI Ltd. All rights reserved.
// ==================================================================================================
// Copyright(C) 2018 FI Ltd. All rights reserved.
//
// SYSTEM NAME      : FI.Net Application Framework
// COMPONENT ID     : FI.Core.EntLib
// COMPONENT DESC   : Microsoft Enterprise Library Adapter.
//
// CREATED DATE/BY  : 18 Dec 2018 / Tian Mi
//
// REVISION HISTORY :
// DATE/BY            SR#/CS/PM#/OTHERS       DESCRIPTION OF CHANGE
// ==================================================================================================
#endregion

using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Logging;
using System;
using System.Diagnostics;

namespace FI.Core.Logging
{
    internal class LoggerWrapper: ILogger
    {
        static LoggerWrapper()
        {
            bool hasGlobalLogWriter = false;
            try
            {
                hasGlobalLogWriter = (null != Logger.Writer);
            }
            catch (InvalidOperationException)
            {
                hasGlobalLogWriter = false;
            }

            if (!hasGlobalLogWriter)
            {
                LogWriterFactory factory = new LogWriterFactory(ConfigurationSourceFactory.Create());
                Logger.SetLogWriter(factory.Create());
            }
        }

        /// <summary>
        /// Logs message.
        /// </summary>
        /// <param name="message">The message to log</param>
        /// <param name="category">The message category</param>
        /// <param name="severity">The message severity</param>
        public void Write(object message, string category, TraceEventType severity)
        {
            Logger.Write(message, category, 0, 0, severity);
        }

        /// <summary>
        /// Logs message.
        /// </summary>
        /// <param name="message">The message to log</param>
        /// <param name="category">The message category</param>
        /// <param name="severity">The message severity</param>
        /// <param name="args">The other parameters for log</param>
        public void Write(object message, string category, TraceEventType severity, params object[] args)
        {
            Logger.Write(message, category, 0, 0, severity);
        }
    }
}
